require "tile.rb"
require "window.rb"

module GUIBase

#The Button class takes a Tile (a rectangular region) and associates it with an image.
#Buttons are not capable of any input or output.

 class Button < Tile
    Image=true
    Rect=false
    @@buttonId=0
    @@instances = []
    attr_reader :type,:src,:buttonId,:imgId,:tileId
    def initialize(type=Rect,x1=0,y1=0,x2=1,y2=1,src="null.png")
      super(x1,y1,x2,y2,true)
      @tileId=@buttonId
      #puts @tileId.class
      #gets
      @type=type 
      @src=src
      @@buttonId+=1
      @buttonId=@@buttonId
      @@instances << self
      if type == Image
        if @scr!="none"
          @imgId=$window.newImage(@x1,@y1,ZOrder::Button,@src,true,"For Button "+@buttonId.to_s)
        end     
      else
        c=Gosu::Color.new(255,255,255,255)
        $window.draw_quad(x1,y1,c,x2,y1,c,x1,y2,c,x2,y2,c,ZOrder::Button) #rect type does not appear to work
      end
    end
    def image
      if @type==Image and @src!="none"
        return $window.images[@imgId]
      else
        return false
      end
    end
    def setImage(nsrc)
      if @type == Image
        if nsrc == @src
          return true
        else
          if @src!="none"
            $window.deleteImage(@imgId)
          end
          @src=nsrc
          @imgId=$window.newImage(@x1,@y1,ZOrder::Button,@src,true,"For Button "+@buttonId.to_s+" (Change)")
          return true
        end
      else
        return false
      end
    end
    def delImage
      if @type == Image and @src!="none"
          $window.deleteImage(@imgId)
          @src = "none"
          return true
      elsif @src=="none"
        return true
      else
        return false
      end     
    end
    def over?(x,y)
      return iTile(x,y)      
    end
    def clicked?(x,y)
      if $window.button_down?(Gosu::Button::MsLeft)
        return xTile(x,y)
      else
        return false
      end
    end
    def del
      if @type==Image and @src!="none"
        $window.deleteImage(@imgId)
      end
      @type,@src,@buttonId,@imgId,@tileId=nil
      super
    end
    def Button.getButton(id)
      return @@instances[id-1]
    end
    def to_s
      return "{[Button]::ID="+@buttonId.to_s+"}"
    end
  end
  
end #module GUIBase

include GUIBase